<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package.Mapper}.${table.mapperName}">

<#if enableCache>
    <!-- 开启二级缓存 -->
    <cache type="${cacheClassName}"/>
</#if>
<#if baseResultMap>
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="${package.Entity}.${entity}">
<#list table.fields as field>
<#if field.keyFlag><#--生成主键排在第一位-->
        <id column="${field.name}" jdbcType="${field.metaInfo.jdbcType}" property="${field.propertyName}" />
</#if>
</#list>
<#list table.commonFields as field><#--生成公共字段 -->
        <result column="${field.name}" jdbcType="${field.metaInfo.jdbcType}" property="${field.propertyName}" />
</#list>
<#list table.fields as field>
<#if !field.keyFlag><#--生成普通字段 -->
        <result column="${field.name}" jdbcType="${field.metaInfo.jdbcType}" property="${field.propertyName}" />
</#if>
</#list>
    </resultMap>

</#if>
<#if baseColumnList>
    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
<#list table.fields as field>${field.name}<#if field_has_next>,</#if></#list>
    </sql>

</#if>

    <insert id="insert" parameterType="${package.Entity}.${entity}">
        insert into ${table.name} (<include refid="Base_Column_List"/>)
        values (<#list table.fields as field><#if field.propertyName="gmtCreate" || field.propertyName="gmtModified"><#if dbType=="oracle">sysdate<#else>NOW()</#if><#else> <#noparse>#{</#noparse>${field.propertyName},jdbcType=${field.metaInfo.jdbcType}<#noparse>}</#noparse></#if><#if field_has_next>,</#if></#list>
        )
    </insert>

    <insert id="batchInsert">
      <#if dbType=="oracle">BEGIN
        <foreach item="item" index="index" collection="list">
            insert into ${table.name} (<include refid="Base_Column_List"/>)
            values (<#list table.fields as field><#if field.propertyName="gmtCreate" || field.propertyName="gmtModified"><#if dbType=="oracle">sysdate<#else>NOW()</#if><#else> <#noparse>#{</#noparse>item.${field.propertyName},jdbcType=${field.metaInfo.jdbcType}<#noparse>}</#noparse></#if><#if field_has_next>,</#if></#list>
            );
        </foreach>
            END;
        <#else>
            insert into ${table.name} (<include refid="Base_Column_List"/>) values
        <foreach item="item" index="index" collection="list">
             (<#list table.fields as field><#if field.propertyName="gmtCreate" || field.propertyName="gmtModified"><#if dbType=="oracle">sysdate<#else>NOW()</#if><#else> <#noparse>#{</#noparse>item.${field.propertyName},jdbcType=${field.metaInfo.jdbcType}<#noparse>}</#noparse></#if><#if field_has_next>,</#if></#list>
            )
        </#if>

    </insert>

    <delete id="deleteByPrimaryKey">
        delete from ${table.name} where
        <#list table.fields as field>
            <#if field.keyFlag>
                ${field.name} = <#noparse>#{</#noparse>id,jdbcType=${field.metaInfo.jdbcType}<#noparse>}</#noparse>
            </#if>
            </#list>
        <#list table.fields as field>
            <#if field.name=="XT_FQID">
                AND XT_FQID = <#noparse>#{</#noparse>divisionId,jdbcType=${field.metaInfo.jdbcType}<#noparse>}</#noparse>
            </#if>
        </#list>

    </delete>

    <delete id="batchDeleteByPrimaryKeys">
        <foreach item="id" index="index" collection="ids">
        delete from ${table.name} where
        <#list table.fields as field>
            <#if field.keyFlag>
                ${field.name} = <#noparse>#{</#noparse>id,jdbcType=${field.metaInfo.jdbcType}<#noparse>}</#noparse>
            </#if>
        </#list>
        <#list table.fields as field>
            <#if field.name=="XT_FQID">
                AND XT_FQID = <#noparse>#{</#noparse>divisionId,jdbcType=${field.metaInfo.jdbcType}<#noparse>}</#noparse>
            </#if>
        </#list>
        </foreach>
    </delete>


    <update id="updateByPrimaryKeySelective" parameterType="${package.Entity}.${entity}">
        update ${table.name}
        <set>
            <#list table.fields as field>
                <#if !field.keyFlag && field.propertyName!="gmtCreate" && field.propertyName!="xtFqid">
            <if test="${field.propertyName} != null">
                ${field.name} =  <#noparse>#{</#noparse>${field.propertyName},jdbcType=${field.metaInfo.jdbcType}<#noparse>}</#noparse><#if field_has_next>,</#if>
            </if>
                </#if>
            </#list>
        </set>
     <#list table.fields as field>
        <#if field.keyFlag>
        where ${field.name} =  <#noparse>#{</#noparse>${field.propertyName},jdbcType=${field.metaInfo.jdbcType}<#noparse>}</#noparse>
        </#if>
     </#list>
        <#list table.fields as field>
            <#if field.name=="XT_FQID">
                AND XT_FQID = <#noparse>#{</#noparse>${field.propertyName},jdbcType=${field.metaInfo.jdbcType}<#noparse>}</#noparse>
            </#if>
        </#list>
    </update>

    <update id="batchUpdateByPrimaryKeySelective">
        <#if dbType=="oracle">BEGIN</#if>
        <foreach item="item" index="index" collection="list">
        update ${table.name}
        <set>
            <#list table.fields as field>
                <#if !field.keyFlag && field.propertyName!="gmtCreate" && field.propertyName!="xtFqid">
                    <if test="item.${field.propertyName} != null">
                        ${field.name} =  <#noparse>#{</#noparse>item.${field.propertyName},jdbcType=${field.metaInfo.jdbcType}<#noparse>}</#noparse><#if field_has_next>,</#if>
                    </if>
                </#if>
            </#list>
        </set>
        <#list table.fields as field>
            <#if field.keyFlag>
                where ${field.name} =  <#noparse>#{</#noparse>item.${field.propertyName},jdbcType=${field.metaInfo.jdbcType}<#noparse>}</#noparse>
            </#if>
        </#list>
        <#list table.fields as field>
            <#if field.name=="XT_FQID">
                AND XT_FQID = <#noparse>#{</#noparse>item.${field.propertyName},jdbcType=${field.metaInfo.jdbcType}<#noparse>}</#noparse>
            </#if>
        </#list> <#if dbType=="oracle">;</#if>
            <#if dbType=="oracle">END;</#if>
        </foreach>
    </update>

    <select id="selectByPrimaryKey" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from ${table.name} where
        <#list table.fields as field>
            <#if field.keyFlag>
                ${field.name} = <#noparse>#{</#noparse>id,jdbcType=${field.metaInfo.jdbcType}<#noparse>}</#noparse>
            </#if>
        </#list>
        <#list table.fields as field>
            <#if field.name=="XT_FQID">
                AND XT_FQID = <#noparse>#{</#noparse>divisionId,jdbcType=${field.metaInfo.jdbcType}<#noparse>}</#noparse>
            </#if>
        </#list>
    </select>

    <#list table.fields as field>
        <#if field.name=="YWZH">
    <select id="selectByYwzh" resultMap="BaseResultMap">
        select * from ${table.name} where
        YWZH = <#noparse>#{</#noparse>ywzh,jdbcType=VARCHAR<#noparse>}</#noparse>
        AND XT_FQID = <#noparse>#{</#noparse>divisionId}<#noparse>}</#noparse>
    </select>

    <update id="updateZtByYwzh">
        update ${table.name} SET ZT=<#noparse>#{</#noparse>zt<#noparse>}</#noparse> where YWZH = <#noparse>#{</#noparse>ywzh<#noparse>}</#noparse> AND XT_FQID = <#noparse>#{</#noparse>divisionId<#noparse>}</#noparse>
    </update>

    <update id="updateGdbjToCompleteByYwzh">
        update ${table.name} SET GDBJ='1',ZT='3',XT_XGSJ=sysdate where YWZH = <#noparse>#{</#noparse>ywzh<#noparse>}</#noparse> AND XT_FQID = <#noparse>#{</#noparse>divisionId<#noparse>}</#noparse>
    </update>
            <#if !field.keyFlag>

    <delete id="deleteByYwzh">
        delete from ${table.name} where
        YWZH = <#noparse>#{</#noparse>ywzh,jdbcType=VARCHAR<#noparse>}</#noparse>
        AND XT_FQID = <#noparse>#{</#noparse>divisionId<#noparse>}</#noparse>
    </delete>
            </#if>
        </#if>
    </#list>

    <#if needVo>
    <!--分页列表-->
    <select id="selectPage" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from ${table.name}
        <where>
            <#list table.fields as field>
                <#if field.propertyName!="gmtCreate" && field.propertyName!="gmtModified">
                    <if test="dto.${field.propertyName} != null and dto.${field.propertyName} != ''">
                        and ${field.name} = <#noparse>#{</#noparse>dto.${field.propertyName},jdbcType=${field.metaInfo.jdbcType}<#noparse>}</#noparse>
                    </if>
                </#if></#list>
        </where>
    </select>
    </#if>
</mapper>
